package main.modelTests;

import java.util.ArrayList;
import java.util.List;

import main.model.persistence.ReadableObject;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Entidad extends ReadableObject{

	private String nombre;

	private List<Atributo> atributos;

	private List<Cardinalidad> cardinalidades;
	
	public Entidad(){
		this.atributos = new ArrayList<Atributo>();
		this.cardinalidades = new ArrayList<Cardinalidad>();
	}

	public void setAtributos(List<Atributo> atributos) {
		this.atributos = atributos;
	}

	public List<Atributo> getAtributos() {
		return atributos;
	}

	public void addAtributo(Atributo a){
		this.atributos.add(a);
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public String getNombre() {
		return nombre;
	}

	@Override
	public void loadObject(Node input) {
		
		NodeList childNodes = input.getChildNodes();
		for (int temp = 0; temp < childNodes.getLength(); temp++) {
			
			Node nNode = childNodes.item(temp);
			
			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				if (nNode.getNodeName().equals("nombre")){
					NodeList childNodes2 = nNode.getChildNodes();
					this.setNombre(childNodes2.item(0).getNodeValue());
				}
				if (nNode.getNodeName().equals("atributos")){
					
					NodeList childAttributes = nNode.getChildNodes();
					for (int index = 0; index < childAttributes.getLength(); index++){
						
						Node childNode = childAttributes.item(index);
						
						if (childNode.getNodeType() == Node.ELEMENT_NODE){

							Atributo att = new Atributo();
							att.loadObject(childNode);
							addAtributo(att);
						
						}

					}
				}
				if (nNode.getNodeName().equals("cardinalidades")){
					
					NodeList childAttributes = nNode.getChildNodes();
					for (int index = 0; index < childAttributes.getLength(); index++){
						
						Node childNode = childAttributes.item(index);
						
						if (childNode.getNodeType() == Node.ELEMENT_NODE){

							Cardinalidad card = new Cardinalidad();
							card.loadObject(childNode);
							addCardinalidad(card);
						
						}

					}
				}
			}
			
		}
	}

	public void addCardinalidad(Cardinalidad c) {
		this.getCardinalidades().add(c);
	}

	public void setCardinalidades(List<Cardinalidad> cardinalidades) {
		this.cardinalidades = cardinalidades;
	}

	public List<Cardinalidad> getCardinalidades() {
		return cardinalidades;
	}


}
